

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'root',
  database : 'wy'
});
connection.connect();
var datas={"code":0,"data":'',"msg":''};
exports.login=function(user,callback){
 var sql="SELECT * from user_info where name='"+user.name+"' and password='"+user.password+"'";
  connection.query(sql,function (err, result) {
    if(err){
      datas.msg=err.message;
      callback(result)
    }else{
      callback(result)
    }
  });
};

/**
 * 获取列表
 * @param  {Function} callback 回调函数
 */
exports.find = function (callback) {
	connection.query('SELECT * from user_info', function (error, results, fields) {
	  if (error) throw error;
	  //console.log('results: ', results);
	  callback(null, results)
	});
}

exports.touSulist = function (callback) {
  connection.query('SELECT * from tousu_info', function (error, results, fields) {
    callback(null, results)
  });
}


/**
 * 根据 id 获取信息对象
 * @param  {Number}   id        id
 * @param  {Function} callback 回调函数
 */
exports.findById = function (id, callback) {
  connection.query("SELECT * from user_info where id="+id, function (error, results, fields){
    callback(null, results[0])
  })
}

/**
 * 添加保存
 * @param  {Object}   user  对象
 * @param  {Function} callback 回调函数
 */
exports.save = function (user, callback) {
  var sql="INSERT INTO user_info( `name`, `password`,phone,nickName,loudong,menpai) VALUES (?,?,?,?,?,?)";
  var  addSqlParams = [user.name, user.password,user.phone,user.nickName,user.loudong,user.menpai];
  connection.query(sql,addSqlParams,function (err,results) {
    callback(null);
  })
}

/**
 * 更新
 */
exports.updateById = function (user, callback) {
 var sql='UPDATE user_info set phone=? ,name=?,nickName=?,password=? ,loudong=? ,menpai=? where id=?';
  var  para = [user.phone,user.name,user.nickName, user.password,user.loudong,user.menpai,user.id];
  connection.query(sql,para,function (err,results) {
    callback(null);
  })
}

/**
 * 删除
 */
exports.deleteById = function (id, callback) {
  var sql="DELETE from user_info where id="+id;
  connection.query(sql,function (err,result) {
    callback(null);
  })
}
